package ioperations;
import book.Book;
import book.bookList;

import java.sql.SQLOutput;
import java.util.Scanner;

public class AddOperatino implements IOperation{
    public void work(bookList BookList) {
        System.out.println("新增图书.....");
        //1.判满
       int useSize = BookList.getUsesize();
       if(useSize == BookList.getBooks().length) {
           System.out.println("书架满了  不能放了");
           return;
       }

       //2.构造对象
        System.out.println("请输入书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入作者");
        String author = scanner.nextLine();
        System.out.println("请输入价格");
        int price = scanner.nextInt();
        System.out.println("请输入类型");
        String type = scanner.nextLine();
        Book newbook = new Book(name,author,price,type);

       //3.判断书架上有没有这本书


        for (int i = 0; i < useSize; i++) {
            Book book = BookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("书架上有这本书");
                System.out.println(book);
                return;
            }
        }

        //4.插入书
        BookList.setBook(useSize,newbook);
        BookList.setUsesize(useSize+1);
        System.out.println("新增图书成功! ! !");
    }
}
